import { Logger } from '@/utils/logger';
import { TaskBase } from '../TaskBase';

export class TaskFortify extends TaskBase {
    shouldRun(creep: Creep): boolean {
        // 确认建筑是 rampart 或者墙壁
        if (
            'structureType' in this.target && (
                this.target.structureType === STRUCTURE_RAMPART ||
                this.target.structureType === STRUCTURE_WALL)
        ) {
            const s = <Structure>this.target;
            if (!this.taskData.amount) {
                this.taskData.amount = Infinity;    // 序列化时，Infinity 被转化为 null，依然可以触发这行代码
            }
            // 还没到要求或者已经到上限
            const needsFortify = s.hits < this.taskData.amount;
            const hasEnergy = creep.store.energy > 0;
            return needsFortify && hasEnergy;
        }
        else {
            // 不是建筑，不运行
            Logger.warn(`试图使用不兼容的目标启动 TaskFortify (${creep.mark}→${this.target})`, 'task');
            return false;
        }
    }
    _run(creep: Creep) {
        return this.defaultProcess(creep.repair(<Structure>this.target));
    }
}
